$(function(){
    var type = getQueryString('type'); // 1 充值  2 提现
    var userInfo = JSON.parse(localStorage.getItem("userInfo"));
    var hylx = getQueryString('a'); // 默认1为充值升级会员等级
    var jlj = getQueryString('b'); // 奖励金余额
    $("#userName").val(userInfo.name);
    $("#tel").val(userInfo.tel);
    // 判断是充值还是提现
    if(type == '1'){
        $("#chongzhi").css('display','block');
        $("#cash").css('display','none');
        $('title').html("充值");
    }
    if(type == '2'){
        $("#chongzhi").css('display','none');
        $("#cash").css('display','block');
        $('title').html("充值");
    }
    // 判断是普通充值还是充值升会员等级
    if(hylx == '1'){ // 高级会员升VIP会员
        $("#price").css("display","block")
        $("#czTit").css("display","block").html("请选择充值金额，立即升级")
    }
    $(".price .item").click(function(){
        $(this).addClass("active").siblings().removeClass("active");
        var price = $(this).attr("data-price")
        $("#czJe").val(price).attr('readonly','readonly');
    });

    // 立即充值
    $("#czBtn").click(function(){
        let czJe = $("#czJe").val();
        let openid = localStorage.getItem("openId");
        let userName = $("#userName").val();
        if(czJe == '' || czJe == null){
            messageBox('请输入充值金额');
            return false;
        }
        $.ajax({
            url: "https://www.zhaodui.com.cn/fxj380/rest/tenPayController/app/tenpay/prepay?openid="+openid+"&total_fees="+czJe+"&body="+userName,
            type: 'GET',
            dataType:'json',
            success:function(e) {
                console.log(JSON.stringify(e));
                var data = e;
                var appId = data.appid;
                var timeStamp = data.timestamp;
                var nonceStr = data.noncestr;
                var packages = data.prepayid;
                var paySign = data.sign;
                pay(appId,timeStamp,nonceStr,packages,paySign);
            }		 
        });
    });

    if(jlj){
        $("#txTit").html('奖励金提现');
    }

    // 立即提现
    $("#txBtn").click(function(){
        let txJe = $("#txJe").val();
        let txMobile = $("#txMobile").val();
        let userName = $("#userName").val();
        // let money = Number(localStorage.getItem("yue"));
        let money = 11;
        if(txJe == '' || txJe == null){
            messageBox('请输入提现金额');
            return false;
        }
        if(txMobile == '' || txMobile == null){
            messageBox('请输入提现手机号');
            return false;
        }
        if(txJe > money){
            messageBox('提现金额不能大于账户余额');
            return false;
        }
        var mark = '充值';
        if(jlj){
            mark = '奖励' 
        }
        var txData = {
            tx01: localStorage.getItem("openId"),
            tx02: userName, // 姓名
            tx03: txMobile, // 手机
            tx04: txJe, // 金额
            tx05: "", // 时间
            tx06: "", 
            tx07: mark,
            tx08: "",
            tx09: "",
        }
        $.ajax({
            type: "POST",
                url: baseUrl + "/rest/lkTixianController",
                contentType:"application/json;charset=UTF-8",
                data: JSON.stringify(txData),
                dataType: "json",
                success: function(e){
                if(e.ok){
                    messageBox('提现成功');
                    setTimeout(function(){
                        window.history.go(-1);
                    },1000)
                }else{
                    messageBox(e.message)
                }

                }
        });
    });
});

// 立即充值
function postCz(){ // 0 初级会员升级高级会员 1 高级会员升级VIP会员 2初级会员升级VIP会员
    let czJe = $("#czJe").val();
    let openid = localStorage.getItem("openId");
    let userName = $("#userName").val();
    let tel = $("#tel").val();
    let idx = $(".price .item.active").attr("data-idx");
    var czData = {
        cz01: openid,
        cz02: userName, // 姓名
        cz03: tel, // 手机
        cz04: czJe, // 金额
        cz05: "", // 时间
        cz06: "",
        cz07: "充值",
        cz08: "",
        cz09: "",
    }
    return false;
    // 充值
    $.ajax({
        type: "POST",
        url: baseUrl + "/rest/lkChongzhiController",
        contentType:"application/json;charset=UTF-8",
        data: JSON.stringify(czData),
        dataType: "json",
        success: function(e){
            if(e.ok){
                messageBox('充值成功！')
                setTimeout(function(){
                    window.history.go(-1);
                },1000)
            }else{
                messageBox('充值失败，请稍后再试')
            }

        }
    });
    // 充值升会员等级
    if(idx == '0'){ // 初级会员升高级会员
        var userInfo = JSON.parse(localStorage.getItem("userInfo"));
        userInfo.type = '高级会员';
        var userInfoA = userInfo;
        $.ajax({
            type: "PUT",
            url: baseUrl + "/rest/lkMyController",
            contentType:"application/json;charset=UTF-8",
            data: JSON.stringify(userInfoA),
            dataType: "json",
            success: function(e){
                if(e.ok){
                    messageBox('充值成功！');
                    $.ajax({
                        type: "GET",
                        url: baseUrl + "/rest/lkMyController/"+localStorage.getItem("openId"),
                        contentType:"application/json;charset=UTF-8",
                        dataType: "json",
                        success: function(data){
                            if(data.ok){
                                if(data.data){
                                    localStorage.setItem("userInfo",JSON.stringify(data.data));
                                    sessionStorage.setItem("userInfo",JSON.stringify(data.data));

                                    setTimeout(function(){
                                        window.history.go(-1);
                                    },1000);
                                }
                            }
                        }
                    });
                   
                }else{
                    messageBox('充值失败，请稍后再试')
                }
    
            }
        });
    }
    if(idx == '1'){ // 高级会员升VIP会员
        var userInfo = JSON.parse(localStorage.getItem("userInfo"));
        userInfo.type = 'VIP会员';
        var userInfoA = userInfo;
        $.ajax({
            type: "PUT",
            url: baseUrl + "/rest/lkMyController",
            contentType:"application/json;charset=UTF-8",
            data: JSON.stringify(userInfoA),
            dataType: "json",
            success: function(e){
                if(e.ok){
                    messageBox('充值成功！');
                    $.ajax({
                        type: "GET",
                        url: baseUrl + "/rest/lkMyController/"+localStorage.getItem("openId"),
                        contentType:"application/json;charset=UTF-8",
                        dataType: "json",
                        success: function(data){
                            if(data.ok){
                                if(data.data){
                                    localStorage.setItem("userInfo",JSON.stringify(data.data));
                                    sessionStorage.setItem("userInfo",JSON.stringify(data.data));

                                    setTimeout(function(){
                                        window.history.go(-1);
                                    },1000);
                                }
                            }
                        }
                    });
                   
                }else{
                    messageBox('充值失败，请稍后再试')
                }
    
            }
        });
    }
    if(idx == '2'){ // 初级会员升VIP会员
        var userInfo = JSON.parse(localStorage.getItem("userInfo"));
        userInfo.type = 'VIP会员';
        var userInfoA = userInfo;
        $.ajax({
            type: "PUT",
            url: baseUrl + "/rest/lkMyController",
            contentType:"application/json;charset=UTF-8",
            data: JSON.stringify(userInfoA),
            dataType: "json",
            success: function(e){
                if(e.ok){
                    messageBox('充值成功！');
                    $.ajax({
                        type: "GET",
                        url: baseUrl + "/rest/lkMyController/"+localStorage.getItem("openId"),
                        contentType:"application/json;charset=UTF-8",
                        dataType: "json",
                        success: function(data){
                            if(data.ok){
                                if(data.data){
                                    localStorage.setItem("userInfo",JSON.stringify(data.data));
                                    sessionStorage.setItem("userInfo",JSON.stringify(data.data));
                                    setTimeout(function(){
                                        window.history.go(-1);
                                    },1000);
                                }
                            }
                        }
                    });
                   
                }else{
                    messageBox('充值失败，请稍后再试')
                }
    
            }
        });
    }
}

//h5唤起微信支付
function onBridgeReady(appId,timeStamp,nonceStr,packages,paySign){
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
            "appId":appId,  //公众号名称，由商户传入     
            "timeStamp":timeStamp,  //时间戳，自1970年以来的秒数     
            "nonceStr":nonceStr,    //随机串     
            "package":"prepay_id="+packages, 
            "signType":"MD5",   //微信签名方式： 
            "paySign":paySign   //微信签名 
        },
        function(res){
            console.log(res.err_code + res.err_desc);
            console.log(res.err_msg);
            // alert(res.err_code + res.err_desc);
            // alert(res.err_msg);
            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                // alert("支持成功-谢谢你对永安基金会的支持，备注：测试系统");// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。//
                // window.location.href = "${ctxPath}/jump/toOrderCommCai";
                postCz(); //
            }else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
                alert("取消支付");
            }
        }
    );  
};
function pay(appId,timeStamp,nonceStr,packages,paySign){  

    if (typeof WeixinJSBridge == "undefined"){  
        if( document.addEventListener ){  
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);  
        }else if (document.attachEvent){  
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);   
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);  
        }  
    }else{  
        onBridgeReady(appId,timeStamp,nonceStr,packages,paySign);  
    }   

};